import * as type from './type'

import axios from 'axios'

const state = {
    list:null
}

export const actionCreators={
    get_classify(){
        //使用了redux-thunk之后，此处应该这么写
        return dispatch=>{//返回一个回调函数
         axios ({
             url:'/cate/catelist',
             params:{
                pf: 8,
                area: 4,
                bi: 222,
                dtype: 'json',
                _: 1566905617850,
                callback: ''
             }
         })
         .then ( data => {
            const action = {
                type:type.GET_CLASSIFY,
                payload:data.data
            }
            dispatch(action)
         } )
         .catch( error => console.log( error ) )
        }     
    }
}

export const reducer=( previousState = state , action)=>{
    const newState = {
        ...previousState
    }
    switch (action.type) {
        case type.GET_CLASSIFY:

            //此处进行数据处理
            const data = action.payload
            data.map( item => {
                item.title = item.name
            } )

            newState.list=data//赋值

            console.log( 'newState 66666',newState )
            break;
        default:
            break;
    }
    return newState//千万别忘了写返回值
}